// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const LevelManager = require('LevelManager');
cc.Class({
    extends: cc.Component,

    properties: {
        wall:{
            type:cc.Node,
            default:null,
            tooltip:'左边刺墙节点'
        },
        wallFrame: {
            type: [cc.SpriteFrame],
            default: [],
            tooltip: '刺墙明暗面图片'
        },
        levelManager:{
            type:LevelManager,
            default:null,
            tooltip:'主角控制组件'
        },
        lightOnTime: {
            type:[cc.Animation],
            tooltip:'明暗同步对应明面动画，注意，明暗节点在数组中要一一对应',
            default:[]
        },
        darkOnTime: {
            type:[cc.Animation],
            tooltip:'明暗同步对应暗面动画，注意，明暗节点在数组中要一一对应',
            default:[]
        },

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.wallBody = this.wall.getComponent(cc.RigidBody);
        // 设置主角一系列回调
        this.levelManager.switchLightCallback = function () {
            var sprite = this.wall.getComponent(cc.Sprite);
            sprite.spriteFrame = this.wallFrame[0];
            this.scheduleLightAnimation();
        }.bind(this);
        this.levelManager.switchDarkCallback = function () {
            var sprite = this.wall.getComponent(cc.Sprite);
            sprite.spriteFrame = this.wallFrame[1];
            this.scheduleDarkAnimation();
        }.bind(this);
        this.levelManager.onStartCallback = function () {
            var speed = this.wallBody.linearVelocity;
            speed.x = 120;
            this.wallBody.linearVelocity = speed;
        }.bind(this);
        this.levelManager.levelPassCallback = function () {
            var speed = this.wallBody.linearVelocity;
            speed.x = 0;
            this.wallBody.linearVelocity = speed;
        }.bind(this);
        this.levelManager.gameOverCallback = function () {
            var speed = this.wallBody.linearVelocity;
            speed.x = 0;
            this.wallBody.linearVelocity = speed;
        }.bind(this);

            
    },

    start () {

    },

    scheduleLightAnimation() {
        for (let i = 0; i < this.darkOnTime.length; i++) {
            var element = this.darkOnTime[i];
            var time = element.getAnimationState(this.darkOnTime[i].defaultClip.name).time;
            time = time % this.darkOnTime[i].defaultClip.duration;
            var name = this.lightOnTime[i].defaultClip.name;
            this.lightOnTime[i].play(name, time);    
        };
    },

    scheduleDarkAnimation() {
        for (let i = 0; i < this.lightOnTime.length; i++) {
            var element = this.lightOnTime[i];
            var time = element.getAnimationState(this.lightOnTime[i].defaultClip.name).time;
            time = time % this.lightOnTime[i].defaultClip.duration;
            var name = this.darkOnTime[i].defaultClip.name;
            this.darkOnTime[i].play(name, time);  
        };
    },
    // update (dt) {},
});
